# This is a sample Python script.

# Press Shift+F10 to execute it or replace it with your code.
# Press Double Shift to search everywhere for classes, files, tool windows, actions, and settings.

import poker
import os
import json


def print_hi(name):
    # Use a breakpoint in the code line below to debug your script.
    print(f'Hi, {name}')  # Press Ctrl+F8 to toggle the breakpoint.


def test_poker_check():
    # 判断皇家同花顺
    assert poker.check_is_royal_flush_straight('AsKsQs', 'JsTs')
    assert poker.check_is_royal_flush_straight('KsQsJs', 'AsTs')
    assert poker.check_is_royal_flush_straight('KsJsTs', 'AsQs')
    assert not poker.check_is_royal_flush_straight('As3s2s', '5s4s')
    assert not poker.check_is_royal_flush_straight('KsJsTs', 'Qs8s')
    assert not poker.check_is_royal_flush_straight('KsJsTs', '9s8s')
    assert not poker.check_is_royal_flush_straight('KsJsTs', 'AsQd')

    # 判断同花顺
    assert poker.check_is_flush_straight('QsJsTs', '9s8s')
    assert poker.check_is_flush_straight('As4s3s', '5s2s')
    assert poker.check_is_flush_straight('8s5s4s', '6s7s')
    assert not poker.check_is_flush_straight('8s5s4s', '9s7s')
    assert not poker.check_is_flush_straight('KsJsTd', 'Qs9s')
    assert not poker.check_is_flush_straight('As4s3s', '6s5s')

    # 判断四条
    assert poker.check_is_quards('AsAdAc', 'Ah8c')
    assert poker.check_is_quards('KsKdQc', 'KhKc')
    assert not poker.check_is_quards('KsKdQc', 'KhQc')
    assert not poker.check_is_quards('KsKdKc', 'QhQc')
    assert not poker.check_is_quards('KsKdKc', 'QhJc')
    assert not poker.check_is_quards('KsKdTc', 'ThTc')

    # 判断葫芦
    assert poker.check_is_full_house('AsAdAc', 'QsQd')
    assert poker.check_is_full_house('AsAdQc', 'QsQd')
    assert poker.check_is_full_house('AsAdQc', 'AcQd')
    assert poker.check_is_full_house('AsTdTc', 'AdAc')
    assert not poker.check_is_full_house('AsAdAc', 'Ah8c')
    assert not poker.check_is_full_house('KsKdQc', 'KhKc')
    assert not poker.check_is_full_house('AsTd9c', 'AdAc')

    # 判断同花
    assert poker.check_is_flush('AsQs9s', '5s4s')
    assert poker.check_is_flush('KsTsQs', 'As4s')
    assert not poker.check_is_flush('KsTsQs', 'As4d')
    assert not poker.check_is_flush('KsTsQd', 'As4s')

    # 判断坚果同花
    assert poker.check_is_nut_flush('AsQs9s', 'Ks4s')
    assert poker.check_is_nut_flush('AsKs4s', 'Qs4s')
    assert poker.check_is_nut_flush('KsTsQs', 'As4s')
    assert poker.check_is_nut_flush('AsKsQs', 'Js4s')
    assert not poker.check_is_nut_flush('AsQs9s', 'Js4s')
    assert not poker.check_is_nut_flush('AsKs4s', '5s4s')
    assert not poker.check_is_nut_flush('KsQsTs', '6s4s')
    assert not poker.check_is_nut_flush('AsKsQs', '9s4s')

    # 判断第二大同花
    assert poker.check_is_second_flush('QsJs9s', 'Ks4s')
    assert poker.check_is_second_flush('AsKs4s', 'Js4s')
    assert poker.check_is_second_flush('KsQsTs', 'Js4s')
    assert poker.check_is_second_flush('AsKsQs', 'Ts4s')
    assert not poker.check_is_second_flush('QsJs9s', 'As4s')
    assert not poker.check_is_second_flush('AsKs4s', 'Ts4s')
    assert not poker.check_is_second_flush('KsQsTs', '5s4s')
    assert not poker.check_is_second_flush('AsKsQs', 'Js4s')

    # 判断顺子
    assert poker.check_is_straight('AsKdQs', 'JsTs')
    assert poker.check_is_straight('KsJs9c', 'QdTs')
    assert poker.check_is_straight('As5c3d', '4s2c')
    assert poker.check_is_straight('Ts8c6d', '9s7c')
    assert not poker.check_is_straight('AsKdQs', 'Js8s')
    assert not poker.check_is_straight('KsJs9c', 'QsQd')
    assert not poker.check_is_straight('As5c3d', '4s4c')
    assert not poker.check_is_straight('Ts8c6d', '9s5c')

    # 判断坚果顺子
    assert poker.check_is_nut_straight('AsKdQs', 'JsTs')
    assert poker.check_is_nut_straight('KdQsTs', 'AsJs')
    assert poker.check_is_nut_straight('QsJs9c', 'KdTs')
    assert poker.check_is_nut_straight('As5c3d', '4s2c')
    assert poker.check_is_nut_straight('Ts8c6d', '9s7c')
    assert poker.check_is_nut_straight('Ts7c6d', '9s8c')
    assert not poker.check_is_nut_straight('QsJs9c', 'Ts8d')
    assert not poker.check_is_nut_straight('KdQsJs', 'Ts9s')

    # 判断是不是下顺
    assert poker.check_is_bottom_straight('KdQsTs', 'Js9d')
    assert poker.check_is_bottom_straight('5c3d2d', 'As4c')
    assert poker.check_is_bottom_straight('Ts8c7d', '9s6c')
    assert not poker.check_is_bottom_straight('AsKdQs', 'JsTs')
    assert not poker.check_is_bottom_straight('KdQsTs', 'AsJs')
    assert not poker.check_is_bottom_straight('QsJs9c', 'KdTs')
    assert not poker.check_is_bottom_straight('As5c3d', '4s2c')
    assert not poker.check_is_bottom_straight('Ts8c6d', '9s7c')
    assert not poker.check_is_bottom_straight('Ts7c6d', '9s8c')

    # 判断set
    assert poker.check_is_set('AsKdQs', 'AdAc')
    assert poker.check_is_set('KsJs9c', '9s9d')
    assert poker.check_is_set('As5c3d', '5s5d')
    assert not poker.check_is_set('As5c3d', '6s6d')
    assert not poker.check_is_set('KsKd9c', '9h9d')
    assert not poker.check_is_set('AsKdQs', 'AdKd')

    # 判断顶set
    assert poker.check_is_top_set('AsKdQs', 'AdAc')
    assert poker.check_is_top_set('KsJs9c', 'KcKh')
    assert poker.check_is_top_set('8s5c3d', '8c8d')
    assert not poker.check_is_top_set('KsJs9c', '9s9d')
    assert not poker.check_is_top_set('KsJs9c', 'JcJd')
    assert not poker.check_is_top_set('AsAdQs', 'AdKd')

    # 判断middle set
    assert poker.check_is_middle_set('AsKdQs', 'KsKc')
    assert poker.check_is_middle_set('KsJs9c', 'JcJh')
    assert poker.check_is_middle_set('8s5c3d', '5s5d')
    assert not poker.check_is_middle_set('KsJs9c', '9s9d')
    assert not poker.check_is_middle_set('KsJs9c', 'KcKd')
    assert not poker.check_is_middle_set('AsAdQs', 'AdKd')

    # 判断bottom set
    assert poker.check_is_bottom_set('AsKdQs', 'QsQc')
    assert poker.check_is_bottom_set('KsJs9c', '9s9h')
    assert poker.check_is_bottom_set('8s5c3d', '3s3c')
    assert not poker.check_is_bottom_set('KsJs9c', 'JsJd')
    assert not poker.check_is_bottom_set('KsJs9c', 'KcKd')
    assert not poker.check_is_bottom_set('AsAdQs', 'AdKd')

    # 判断明三条
    assert poker.check_is_trips('AsKdKs', 'KsQc')
    assert poker.check_is_trips('KsKd9c', 'Ks8h')
    assert poker.check_is_trips('8s5c5d', 'As5s')
    assert not poker.check_is_trips('KsJs9c', 'JsJd')
    assert not poker.check_is_trips('KsKd9c', 'Kc9d')
    assert not poker.check_is_trips('8s5c5d', '8c7d')

    # 判断两对
    assert poker.check_is_two_pair('AsKdQs', 'AcKs')
    assert poker.check_is_two_pair('KsJs9c', 'Kc9h')
    assert poker.check_is_two_pair('8s5c3d', '5s3c')
    assert not poker.check_is_two_pair('AsKdKs', 'AcQs')
    assert not poker.check_is_two_pair('AsKdQs', 'KcKs')
    assert not poker.check_is_two_pair('KsJs9c', 'Kc8h')
    assert not poker.check_is_two_pair('8s5c3d', 'As6c')

    # 判断顶两对
    assert poker.check_is_top_two_pair('AsKdQs', 'AcKs')
    assert poker.check_is_top_two_pair('KsJs9c', 'KcJh')
    assert poker.check_is_top_two_pair('8s5c3d', '8c5s')
    assert not poker.check_is_top_two_pair('AsKdKs', 'AcQs')
    assert not poker.check_is_top_two_pair('AsKdQs', 'KcQs')
    assert not poker.check_is_top_two_pair('KsJs9c', 'Kc9h')

    # 判断中间两对
    assert poker.check_is_middle_two_pair('AsKdQs', 'AcQs')
    assert poker.check_is_middle_two_pair('KsJs9c', 'Kc9h')
    assert poker.check_is_middle_two_pair('8s5c3d', '8c3s')
    assert not poker.check_is_middle_two_pair('AsKdKs', 'AcQs')
    assert not poker.check_is_middle_two_pair('AsKdQs', 'KcQs')
    assert not poker.check_is_middle_two_pair('KsJs9c', 'KcJh')

    # 判断底两对
    assert poker.check_is_bottom_two_pair('AsKdQs', 'KcQs')
    assert poker.check_is_bottom_two_pair('KsJs9c', 'Jc9h')
    assert poker.check_is_bottom_two_pair('8s5c3d', '5c3s')
    assert not poker.check_is_bottom_two_pair('AsKdKs', 'AcQs')
    assert not poker.check_is_bottom_two_pair('AsKdQs', 'AcQs')
    assert not poker.check_is_bottom_two_pair('KsJs9c', 'KcJh')

    # 判断超对
    assert poker.check_is_over_pair('KsJs9c', 'AcAh')
    assert poker.check_is_over_pair('KsJsJc', 'AcAh')
    assert poker.check_is_over_pair('8s5c3d', 'TcTs')
    assert poker.check_is_over_pair('8s8c3d', 'TcTs')
    assert not poker.check_is_over_pair('AsKdKs', 'QcQs')
    assert not poker.check_is_over_pair('AsKdKs', 'AcAs')
    assert not poker.check_is_over_pair('AsKdQs', 'AcQs')
    assert not poker.check_is_over_pair('KsJs9c', '8c8h')
    assert not poker.check_is_over_pair('8s8c3d', '7c7s')

    # 判断超对AA
    assert poker.check_is_over_pair_aa('8s6c5d', 'AdAc')

    # 判断超对KK
    assert poker.check_is_over_pair_kk('8s6c5d', 'KdKc')
    assert not poker.check_is_over_pair_kk('As6c5d', 'KdKc')

    # 判断顶对
    assert poker.check_is_top_pair('AsKsQs', 'AdJd')
    assert poker.check_is_top_pair('8s6c5d', 'Ad8d')
    assert poker.check_is_top_pair('8s6c5d', '8d7c')
    assert poker.check_is_top_pair('8s6c6d', 'Ad8d')
    assert not poker.check_is_top_pair('8s6c5d', '7c5d')
    assert not poker.check_is_top_pair('8s6c5d', '8d6c')
    assert not poker.check_is_top_pair('8s6c5d', 'Ad5d')
    assert not poker.check_is_top_pair('8s6c5d', 'Ad9d')

    # 判断顶对和中队之间的对子
    assert poker.check_is_pair_between_top_and_middle('AsQsTs', 'KsKd')
    assert poker.check_is_pair_between_top_and_middle('KsTsTd', 'QdQh')
    assert poker.check_is_pair_between_top_and_middle('8s6s3s', '7s7d')
    assert poker.check_is_pair_between_top_and_middle('AsQsTs', 'KsKd')
    assert not poker.check_is_pair_between_top_and_middle('QsQdQh', 'JsJd')
    assert not poker.check_is_pair_between_top_and_middle('AsKsQs', 'KdJd')
    assert not poker.check_is_pair_between_top_and_middle('KsQsJs', 'KdKd')
    assert not poker.check_is_pair_between_top_and_middle('KsQsTs', 'JdJd')

    # 判断中对
    assert poker.check_is_middle_pair('AsKsQs', 'KdJd')
    assert poker.check_is_middle_pair('8s6c5d', 'Ad6d')
    assert poker.check_is_middle_pair('8s6c5d', '7c6d')
    assert poker.check_is_middle_pair('8s8c5d', '7c5d')
    assert not poker.check_is_middle_pair('8s6c5d', '6c5d')
    assert not poker.check_is_middle_pair('8s6c5d', 'Ad5d')
    assert not poker.check_is_middle_pair('8s6c5d', 'Ad8d')

    # 判断中队和底对之间的对子
    assert poker.check_is_pair_between_middle_and_bottom('AsQsTs', 'JsJd')
    assert poker.check_is_pair_between_middle_and_bottom('8s6s3s', '5s5d')
    assert not poker.check_is_pair_between_middle_and_bottom('QsQdQh', 'JsJd')
    assert not poker.check_is_pair_between_middle_and_bottom('AsKsQs', 'KdJd')
    assert not poker.check_is_pair_between_middle_and_bottom('KsQsJs', 'KdKd')
    assert not poker.check_is_pair_between_middle_and_bottom('AsQsTs', 'KdKh')

    # 判断底对
    assert poker.check_is_bottom_pair('AsKsQs', 'QdJd')
    assert poker.check_is_bottom_pair('8s6c5d', 'Ad5d')
    assert poker.check_is_bottom_pair('8s6c5d', '7c5d')
    assert not poker.check_is_bottom_pair('8s8c5d', '7c5d')
    assert not poker.check_is_bottom_pair('8s6c5d', '6c5d')
    assert not poker.check_is_bottom_pair('8s6c5d', 'Ad6d')
    assert not poker.check_is_bottom_pair('8s6c5d', 'Ad8d')

    # 判断弱队（小于底对）
    assert poker.check_is_weak_pair('AsQsTs', '9s9d')
    assert poker.check_is_weak_pair('KsTsTd', '9d9h')
    assert poker.check_is_weak_pair('8s6s3s', '2s2d')
    assert poker.check_is_weak_pair('AsQsTs', '8s8d')
    assert not poker.check_is_weak_pair('AsQsTs', 'JsJd')
    assert not poker.check_is_weak_pair('KsTsTd', 'JdJh')
    assert not poker.check_is_weak_pair('8s6s3s', '4s4d')
    assert not poker.check_is_weak_pair('AsQsTs', 'KsKd')
    assert not poker.check_is_weak_pair('QsQdQh', 'JsJd')
    assert not poker.check_is_weak_pair('AsKsQs', 'KdJd')
    assert not poker.check_is_weak_pair('KsQsJs', 'KdKd')
    assert not poker.check_is_weak_pair('KsQsTs', 'JdJd')

    # 判断同花听牌
    assert poker.check_is_flush_draw('AsQsTd', '9s8s')
    assert poker.check_is_flush_draw('AsQsTs', '9s8d')
    assert poker.check_is_flush_draw('AsQdTs', '9s8s')
    assert poker.check_is_flush_draw('AsQdTs', '9s8s')
    assert not poker.check_is_flush_draw('AsQsTd', '9s8d')
    assert not poker.check_is_flush_draw('AsQsTd', '9s8c')
    assert not poker.check_is_flush_draw('AsQdTs', '9d8d')
    assert not poker.check_is_flush_draw('AsQsTs', '9d8d')

    # 判断Oesd
    assert poker.check_is_oesd('8s9sQd', '6d7d')
    assert poker.check_is_oesd('8s9sTd', 'Ts7d')
    assert poker.check_is_oesd('8s9sTd', 'Qs6d')
    assert poker.check_is_oesd('8sTsQd', '9s6d')
    assert poker.check_is_oesd('AsQsJd', 'Ts8d')
    assert poker.check_is_oesd('AsQsJd', 'Ts9d')
    assert not poker.check_is_oesd('8s9sQd', 'Jd7d')
    assert not poker.check_is_oesd('8s9sQd', '6d5d')
    assert not poker.check_is_oesd('8s9sQd', '9dTd')
    assert not poker.check_is_oesd('AsQsJd', 'KsQd')
    assert not poker.check_is_oesd('As4s3d', '4s2d')

    # 判断卡顺
    assert poker.check_is_gut_shot('8s9sQd', 'Jd7d')
    assert poker.check_is_gut_shot('8s9sTd', 'TsQd')
    assert poker.check_is_gut_shot('AsQsJd', 'Ts7d')
    assert poker.check_is_gut_shot('AsQsJd', 'Ks9d')
    assert poker.check_is_gut_shot('As4s3d', 'Ks5d')
    assert poker.check_is_gut_shot('As4s3d', 'Ks2d')
    assert poker.check_is_gut_shot('AsQsJd', 'Ks8d')
    assert not poker.check_is_gut_shot('8s9sQd', '6d7d')
    assert not poker.check_is_gut_shot('8s9sTd', 'Ts7d')
    assert not poker.check_is_gut_shot('8s9sTd', 'Qs6d')
    assert not poker.check_is_gut_shot('8sTsQd', '9s6d')
    assert not poker.check_is_gut_shot('AsQsJd', 'Ts8d')
    assert not poker.check_is_gut_shot('AsQsJd', 'Ts9d')
    assert not poker.check_is_gut_shot('As4s3d', 'Ks6d')

    # 判断后门花
    assert poker.check_is_backdoor_flush('8s9sQd', 'Jd7d')
    assert poker.check_is_backdoor_flush('8s9sQd', 'Js7d')
    assert poker.check_is_backdoor_flush('8s9dQc', 'Js7s')
    assert not poker.check_is_backdoor_flush('8s9sQd', 'Js7s')
    assert not poker.check_is_backdoor_flush('8s9sQd', 'Jc7d')
    assert not poker.check_is_backdoor_flush('8s9sQs', 'Jc7d')


def generate_board_analyze(file_full_path, board):
    board_strategy = {}
    with open(file_full_path, 'r') as f:
        lines = f.readlines()
        for line in lines:
            if line == '\n':
                break
            if line[-1] == '\n':
                line = line[:-1]
            if line[-1] == '\t':
                line = line[:-1]
            line = line.replace('\t\t\t', '\t')
            line = line.replace('\t\t', '\t')
            hand_strategy_array = line.split('\t')
            hand = hand_strategy_array[0]
            hand_desc = poker.generate_hand_desc(board, hand)
            bet_strategy = {}
            array_len = len(hand_strategy_array)
            bet_count = (array_len - 4) / 3
            if bet_count == 4:
                bet_strategy['b_b'] = round(float(hand_strategy_array[4 + 3]), 2)
                bet_strategy['b_m'] = round(float(hand_strategy_array[4 + 4]), 2)
                bet_strategy['b_s'] = round(float(hand_strategy_array[4 + 5]), 2)
                bet_strategy['c'] = round(float(hand_strategy_array[4 + 6]), 2)
            elif bet_count == 5:
                bet_strategy['r_b'] = round(float(hand_strategy_array[5 + 3]), 2)
                bet_strategy['r_m'] = round(float(hand_strategy_array[5 + 4]), 2)
                bet_strategy['r_s'] = round(float(hand_strategy_array[5 + 5]), 2)
                bet_strategy['cl'] = round(float(hand_strategy_array[5 + 6]), 2)
                bet_strategy['f'] = round(float(hand_strategy_array[5 + 7]), 2)
            elif bet_count == 6:
                bet_strategy['r_a'] = round(float(hand_strategy_array[6 + 3]), 2)
                bet_strategy['r_b'] = round(float(hand_strategy_array[6 + 4]), 2)
                bet_strategy['r_m'] = round(float(hand_strategy_array[6 + 5]), 2)
                bet_strategy['r_s'] = round(float(hand_strategy_array[6 + 6]), 2)
                bet_strategy['cl'] = round(float(hand_strategy_array[6 + 7]), 2)
                bet_strategy['f'] = round(float(hand_strategy_array[6 + 8]), 2)
            board_strategy[hand] = {
                'eq': round(float(hand_strategy_array[1]), 2),
                'combos': round(float(hand_strategy_array[2]), 3),
                'desc': hand_desc,
                'bet': bet_strategy
            }
    return board_strategy


def generate_hand_analyze(dir_full_path):
    hand_analyze = {}
    for parent, dirs, files in os.walk(dir_full_path):
        for file in files:
            file_full_path = os.path.join(parent, file)
            board = file[:-4]
            hand_analyze[board] = generate_board_analyze(file_full_path, board)
    return hand_analyze


def generate_category_hand_analyze(folder_path, folder_name):
    category_hand_analyze = {}
    for parent, dirs, files in os.walk(folder_path):
        for dir in dirs:
            file_full_path = os.path.join(folder_path, dir)
            category_hand_analyze[dir] = generate_hand_analyze(file_full_path)

    if not os.path.exists('hand_strategies/' + folder_name):
        os.makedirs('hand_strategies/' + folder_name)
    for key in category_hand_analyze:
        with open('hand_strategies/' + folder_name + '/' + key + '.json', 'w') as fJson:
            fJson.write(json.dumps(category_hand_analyze[key]))


# Press the green button in the gutter to run the script.
if __name__ == '__main__':
    dirs = os.listdir('origin')
    for dir in dirs:
        generate_category_hand_analyze('origin/' + dir, dir)

    print_hi('1')

# See PyCharm help at https://www.jetbrains.com/help/pycharm/
